const {Router} = require('express')
let router = new Router()
const upload = require("../lib/upload")
const fs = require('fs');


router.post('/image', upload.single('file'), (req, res) => {
        let name = req.file.filename || req.file.name;
        const url = 'http://localhost:3000/upload/' + name;
        res.json({ url })
    })
router.get('/upload/*', (req, res) => {
        res.setHeader('Content-Type', 'image/png,image/jpg');
        res.sendFile(__dirname + req.url);
})

// 删除图片操作
router.get('/image/delete', (req, res) => {
    console.log(req)
    let filePath = __dirname + '/upload/' + req.query.filename;
    fs.unlink(filePath, (err) => {
        if (!err) {
            res.send({
                success: true,
                msg: '删除成功'
            })
        } else {
            res.send({
                success: false,
                msg: '删除失败' + err
            })
        }
    })
})

module.exports = router



